extends PopupDialog

onready var level_manager : LevelsManager = $"/root/Main/Level"

onready var level_label : Label = $"CurrentLevel"

onready var level_screenshot : TextureRect = $"Screenshot"
	
var display_level : int = -1	

func show():
	var current_level = level_manager.level
	set_screenshot_by_level(current_level)
	visible = true
	
func _on_Close_pressed():
	visible = false

# 设置屏幕截图
func set_screenshot_by_level(level:int) -> void:
	level_label.text = "魔塔 第%0*dF" % [2,level]
	var image : Image = null
	if level == level_manager.level:
		image = get_viewport().get_texture().get_data()
	else:
		image = Image.new()
		image.load(level_manager.get_screenshot_image_path(level))
	image.flip_y()
	var texture = ImageTexture.new()
	texture.create_from_image(image,image.get_format())
	level_screenshot.texture = texture
	display_level = level

# 点击增加楼层
func _on_AddLevel_pressed():
	if level_manager.get_go_to_levels().has(display_level + 1):
		set_screenshot_by_level(display_level + 1)

# 点击减去楼层
func _on_SubLevel_pressed():
	if level_manager.get_go_to_levels().has(display_level - 1):
		set_screenshot_by_level(display_level - 1)

# 点击楼层截图视图回调
func _on_Screenshot_gui_input(event):
	if event is InputEventMouseButton and event.pressed:
		visible = false
		level_manager.set_level(display_level)
